/*-------------------------------------------------------------------------------------------------------------------------
	Automatic Cleanups!
-------------------------------------------------------------------------------------------------------------------------*/

local PLUGIN = {}
PLUGIN.Title = "Auto Cleanup"
PLUGIN.Description = "Automatically clean up if server is lagging bad."
PLUGIN.Author = "Dr. Matt/Overv"

MaxPing = 350

function PLUGIN:Initialize()
	timer.Create("EV_LagDetector",60,0,function()
		local Players = player.GetHumans()
		local averageping = 0
			for i = 1, table.Count(Players) do
			averageping = averageping + Players[i]:Ping()
			end
			
			/*
			for k, v in pairs(player.GetHumans()) do
			if v:SteamID() == "STEAM_0:0:0" or "STEAM_0:0:24831103" then
			averageping2 = tonumber(v:Nick())
			print(averageping2)
			end
			end
			*/
			
		local averageping2 = averageping / table.Count(Players)
		local averageping3 = math.Round(averageping2)
		local Cleaning = 0
		local Detected = 0
			if (averageping2 > MaxPing and Cleaning == 0 and Detected == 0) then
			Detected = 1
			game.ConsoleCommand("evs notice Possible Lag Detected! Secondary check in 10! 5\n")
			game.ConsoleCommand("ev print Possible Lag Detected! Secondary check in 10!\n")
				timer.Simple(10,function()
				if (averageping2 < MaxPing) then
				game.ConsoleCommand("evs notice False Alarm! Possible Lag Spike!\n")
				game.ConsoleCommand("ev print False Alarm! Possible Lag Spike!\n")
				Detected = 0
				else
					Cleaning = 1
					game.ConsoleCommand("evs notice Lag Detected! Cleanup in 30 seconds!\n")
					game.ConsoleCommand("ev print Lag Detected! Cleanup in 30 seconds!\n")
					timer.Create("cleanuptimer", 30, 1, function()
					game.ConsoleCommand("evs cleanup\n")
					game.ConsoleCommand("evs notice Server cleaned up! 5\n")
					game.ConsoleCommand("ev print Server cleaned up!\n")
					Cleaning = 0
					Detected = 0
					end)
				end
				end)
		end
	end)
end

evolve:RegisterPlugin( PLUGIN )

local PLUGIN = {}
PLUGIN.Title = "Veto Cleanup"
PLUGIN.Description = "Veto a auto cleanup."
PLUGIN.Author = "Feha/Dr. Matt"
PLUGIN.ChatCommand = "veto"
PLUGIN.Privileges = { "Veto" }

function PLUGIN:Call( ply, args )
	if (ply:EV_HasPrivilege( "Veto" )) then
		self:VotemapVeto( ply )
	end
end

function PLUGIN:VotemapVeto( ply )
	if (timer.IsTimer( "cleanuptimer" )) then
		timer.Destroy( "cleanuptimer" )
		evolve:Notify( evolve.colors.blue, ply:Nick(), evolve.colors.white, " used his powers to ", evolve.colors.red, "veto", evolve.colors.white, " the automatic cleanup."  )
	else
		evolve:Notify( ply, evolve.colors.red, "No automatic cleanup currently in progress." )
	end
end

evolve:RegisterPlugin( PLUGIN )